package com.mofang.powerdekorhelper.activity.order;

import android.app.Dialog;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.adapter.OrderAdapter;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewList;
import com.mofang.powerdekorhelper.model.Order;
import com.mofang.powerdekorhelper.model.OrderType;
import com.mofang.powerdekorhelper.model.ResultMessage;
import com.mofang.powerdekorhelper.persenter.OrderManagerPersenter;
import com.mofang.powerdekorhelper.utils.DialogUtils;
import com.mofang.powerdekorhelper.utils.L;
import com.mofang.powerdekorhelper.utils.LoadPrograss;
import com.mofang.powerdekorhelper.utils.ParamsUtils;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.OrderManagerView;
import com.mofang.powerdekorhelper.witget.MySwipeRefreshLayout;
import com.mofang.powerdekorhelper.witget.TitleBar;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.mofang.powerdekorhelper.activity.order.OrderEditActivity.ToDetailCode;

/**
 * Created by luofei on 2017/8/23 0023.
 * 订单列表
 */

public class OrderManagerActivity extends MvpActivity<OrderManagerView, OrderManagerPersenter> implements
        OrderManagerView,
        TabLayout.OnTabSelectedListener,
        SwipeRefreshLayout.OnRefreshListener,
        OrderAdapter.IOrderClick,
        BaseRecycleViewList.MyRecycleOnScrollListener {

    private final String TAG = this.getClass().getSimpleName();

    @BindView(R.id.order_manager_titlebar)
    TitleBar mTitleBar;

    @BindView(R.id.order_manager_tab_nav)
    TabLayout mTabLayout;

    @BindView(R.id.order_manager_list)
    BaseRecycleViewList orderList;

    @BindView(R.id.order_refresh_layout)
    MySwipeRefreshLayout refreshLayout;

    @BindView(R.id.order_search_edit)
    EditText searchEdit;

    private OrderAdapter adapter;

    private LoadPrograss loadPrograss;

    Dialog commitDialog = null;

    private boolean isLoadAll;

    int page = 1;
    int pageSize = 5;
    boolean isPage = true;
    Integer orderState = null;
    String searchKeyWord = "";
    // int orderType;
    int userId;

    @Override
    public void initView() {
        inflateLayout(R.layout.activity_order_manager_layout);
        initTitleBarWithback(mTitleBar, R.string.order_manager_title);
        mTitleBar.setDividerColor(R.color.grey_midle);
        loadPrograss = new LoadPrograss(this);
        refreshLayout.setOnRefreshListener(this);
        orderList.setListOnScrollListener(this);
    }

    @Override
    public void initData() {
        userId = SharePerforenceUtils.getInstance(this).getUserID();
        presenter.getOrderType();
        presenter.getOrderManagerList(ParamsUtils.getOrderListParams(page, pageSize, isPage, userId, orderState, getSearchKeyWord()));

        searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    page = 1;
                    presenter.getOrderManagerList(ParamsUtils.getOrderListParams(page, pageSize, isPage, userId, orderState, getSearchKeyWord()));
                }
                return false;
            }
        });
    }

    @OnClick({R.id.order_search_btn})
    void onclick(View v) {
        switch (v.getId()) {
            case R.id.order_search_btn:
                page = 1;
                presenter.getOrderManagerList(ParamsUtils.getOrderListParams(page, pageSize, isPage, userId, orderState, getSearchKeyWord()));
                break;

        }
    }

    @Override
    public void loadMore() {

        L.i("OrderManagerPersenter", "loadMore======================>");
        if (!isLoadAll) {
            presenter.getOrderManagerList(ParamsUtils.getOrderListParams(++page, pageSize, isPage, userId, orderState, getSearchKeyWord()));
        }
    }

    // Spiner选中
   /* @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        page = 1;
        isLoadAll = false;
        presenter.getOrderType();
        presenter.getOrderManagerList(ParamsUtils.getOrderListParams(page, pageSize, isPage, userId, orderState, getSearchKeyWord()));
    }*/

    // 刷新
    @Override
    public void onRefresh() {
        page = 1;
        isLoadAll = false;
        presenter.getOrderManagerList(ParamsUtils.getOrderListParams(page, pageSize, isPage, userId, orderState, getSearchKeyWord()));
    }

    // 订单类型
    @Override
    public void setOrderType(OrderType orderType) {
        List<OrderType.Data> datas = orderType.getData();

        mTabLayout.removeAllTabs();

        for (OrderType.Data data : datas) {
            mTabLayout.addTab(mTabLayout.newTab().setText(data.getOrderName()).setTag(data.getOrderId()));
        }

        mTabLayout.setOnTabSelectedListener(this);
    }

    // 订单数据
    public void setOrderList(Order order) {
        if (page == 1) {
            adapter = new OrderAdapter(order.getResult().getData(), this, R.layout.order_list_item_layout);
            adapter.setOrderClick(this);
            orderList.setAdapter(adapter);
        } else {
            adapter.addData(order.getResult().getData());
            if (order.getResult().getData().isEmpty()) {
                isLoadAll = true;
            }
        }

        hideProgress();
    }

    @Override
    public void setCloseOrder(ResultMessage resultMessage) {
        hideProgress();
        commitDialog.dismiss();
        toast("关闭订单成功");
    }

    // tab监听
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        page = 1;
        isLoadAll = false;
        orderState = (Integer) tab.getTag();
        presenter.getOrderManagerList(ParamsUtils.getOrderListParams(page, pageSize, isPage, userId, orderState, getSearchKeyWord()));
    }

    // 刷新回调
    @Override
    public void orderRefresh(int orderId, String orderCode, int activityId, int customerId) {
        this.orderId = orderId;
        presenter.getRefreshOrder(ParamsUtils.getRefreshOrderParams(orderId, orderCode, activityId, customerId));
    }

    int orderId;

    @Override
    public void setRefreshOrder(ResultMessage refreshOrder) {

        toast("刷新订单成功");
        hideProgress();

        startActivity(new Intent(OrderManagerActivity.this, OrderEditActivity.class)
                .putExtra("OrderId", orderId).putExtra("EnterFlag", 2));
    }

    // 详情回调
    @Override
    public void orderDetail(int orderId) {
        startActivity(new Intent(OrderManagerActivity.this, OrderDetailActivity.class)
                .putExtra("OrderId", orderId));
    }

    // 关闭回调
    @Override
    public void orderClose(final int orderId, int orderState) {

        commitDialog = DialogUtils.getInstance().CommitInfoDialog(this, new DialogUtils.IgetCommitInfo() {
            @Override
            public void getCommitInfo(String commitInfo) {

                if (!commitInfo.trim().equals("")) {
                    presenter.getCloseOrder(ParamsUtils.getCloseOrder(orderId, commitInfo));
                } else {
                    toast("请输入关闭理由");
                }
            }

            @Override
            public void cancleDialog() {
                commitDialog.dismiss();
            }
        });
        commitDialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {

            case ToDetailCode:

                break;
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void showProgress() {
        loadPrograss.show();
    }

    @Override
    public void hideProgress() {
        loadPrograss.hide();
        refreshLayout.hide();
    }

    @Override
    public void toast(CharSequence s) {
        T.showShort(this, s);
    }

    @Override
    public void toast(int id) {
        T.showShort(this, getResources().getString(id));
    }

    @Override
    public void showNullLayout() {
        hideProgress();
        toast(R.string.no_data);
    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {

    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {
        hideProgress();
        toast(errorInfo);
    }

    @Override
    public OrderManagerPersenter initPresenter() {
        return new OrderManagerPersenter();
    }

    private String getSearchKeyWord() {
        searchKeyWord = searchEdit.getText().toString().trim();
        return searchKeyWord;
    }

}
